package corpus;

import java.util.ArrayList;

import model.Classe;
import model.VariableStatiques;
import weka.core.Attribute;
import weka.core.FastVector;
import weka.core.Instance;
import weka.core.Instances;

public class CreateInstance {

	public static final String CLASSENAME = "#!NOMCLASSES!#";
	public CreateInstance () {

	}

	private static Instances init (){
		FastVector attributes = new FastVector();
		FastVector lesClasses = new FastVector();
		int nbClasses = VariableStatiques.LIST_CLASSES.size();
		for(int i=0 ; i<nbClasses; i++){
			lesClasses.addElement(VariableStatiques.LIST_CLASSES.get(i).getNomClasse());
		}
		attributes.addElement(new Attribute("text", (FastVector)null));
		attributes.addElement(new Attribute(CLASSENAME, lesClasses));
		Instances inst = new Instances("instances", attributes, 0);
		inst.setClassIndex(inst.numAttributes() -1);
		return inst;
	}

	private static void addTexts(Instances inst, Classe classes, int numClass){
		int nbTextes = classes.getTextes().size();
		for (int i=0; i<nbTextes; i++){
			double[] newInstance = new double [2];
			newInstance[0] = (double) inst.attribute(0).addStringValue(classes.getTextes().get(i).getTexte());

			if(numClass > -1)
				newInstance[inst.classIndex()] = (double) numClass;
			else 
				newInstance[inst.classIndex()] = Instance.missingValue();

			inst.add(new Instance(1.0, newInstance));
		}

	}


	public static Instances CreateInstance (ArrayList<Classe> classes){
		int nbClasse = classes.size();
		VariableStatiques.LIST_CLASSES_NAME = new ArrayList<String> ();

		for (int i=0; i<nbClasse; i++){
			VariableStatiques.LIST_CLASSES_NAME.add(classes.get(i).getNomClasse());
		}
		Instances inst = init();

		for(int i=0; i<nbClasse; i++){
			addTexts(inst, classes.get(i), i);
		}

		return inst;
	}

	public static Instances createInstancesSearch(ArrayList<Classe> classesText){
		int nbClasse = classesText.size();
		Instances inst = init();
		for(int i=0; i< nbClasse; i++){
			if(VariableStatiques.LIST_CLASSES_NAME.contains(classesText.get(i).getNomClasse())){
				addTexts(inst, classesText.get(i), VariableStatiques.LIST_CLASSES_NAME.indexOf(classesText.get(i).getNomClasse()));
			}
			else{
				addTexts(inst, classesText.get(i), -1);
			}
		}
		return inst;
	} 
}
